// pages/twozc/twozc.js
const app = getApp();
Page({
  data: {
    openid: '',
    mealsList: '',
    USER_ASSETS_NAME: '', //姓名

    BANK_ASSETS: '', //资产
    EMPLOYEE_ID: '', //工号
    showView: false,
    showView_a: true, //选择金额
    yuangong: '',
    USER_ASSETS_IDCARD: '', //手机号
    ACTIVITY_TYPE: '', //奖券金额
    USER_IDCARD: '', //身份证号
    flag: true, //节流
    UUIDSTRING: '',
    allList: [{
        moshi: '开卡',
        zhuangtai: 1,
      },
      {
        moshi: '存款',
        zhuangtai: 2,
      },
    ],


    cunkuanxuan: [{
        cunqu: '存期1年',
        cunquan: 20,
      },
      {
        cunqu: '存期半年',
        cunquan: 10,
      },
      {
        cunqu: '存期3月',
        cunquan: 5,
      },

    ],

    cunsi: true,

    ACTIVITY_REGULARLY: 0,
    ACTIVITY_AMOUNT: 0,
    xunriqi: true,

    moshi: '',
    cunqu: '',
  },








  //拿到员工id
  onLoad: function (e) {
    console.log(e);
    this.setData({
      mealsList: app.globalData.mealsList,
      UUIDSTRING: app.globalData.UUIDSTRING,
      openid: app.globalData.openid,
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      session_key: app.globalData.session_key,

    });
    console.log(e.data);
    var yuan = e.data
    this.setData({
      yuangong: yuan
    })
    console.log(this.data.yuangong);
    console.log(this.data.mealsList);

  },


  //判断输入框是否有数据

  jump: function () {
    if ( !this.data.USER_ASSETS_NAME || !this.data.USER_ASSETS_IDCARD || !this.data.mealsList || !this.data.ACTIVITY_TYPE) {
      console.log(this.data.USER_ASSETS_NAME);
      console.log(this.data.USER_ASSETS_IDCARD);
      console.log(this.data.mealsList);
      console.log(this.data.ACTIVITY_AMOUNT);
      console.log(this.data.ACTIVITY_REGULARLY);
      console.log(this.data.ACTIVITY_TYPE);
      wx.showModal({
        title: '温馨提示',
        content: '请完善信息',
        success: function (res) {
          if (res.confirm) { //这里是点击了确定以后   
            console.log('用户点击确定')
          } else { //这里是点击了取消以后
            console.log('用户点击取消')
          }
        }
      })

    } else {
      this.setData({
        showView: (!this.data.showView)
      })
    }
  },

  //跳转回去
  quno: function () {
    wx.redirectTo({
      url: '../../pages/weinanbaishuiluru/weinanbaishuiluru'
    })
  },


  //点击确定以后向后台传数据
  tjok: function () {
    if (this.data.flag) {
      console.log(111);

      wx.request({
        url: app.globalData.interface+'/weinanpost/WeiNanPostAPI_1003_AddBaiShuiApple',
        method: 'POST',
        data: {
          USER_ASSETS_NAME: this.data.USER_ASSETS_NAME, //姓名
          USER_ASSETS_IDCARD: this.data.USER_ASSETS_IDCARD, //手机号
          EMPLOYEE_ID: this.data.mealsList, //员工工号
          OPEN_ID: this.data.openid, //用户OPEN_ID
          session_key: this.data.session_key,
          OPEN_UUID: this.data.jiami,
          ACTIVITY_REGULARLY: this.data.ACTIVITY_REGULARLY,
          ACTIVITY_AMOUNT: this.data.ACTIVITY_AMOUNT,
          ACTIVITY_TYPE: this.data.ACTIVITY_TYPE,

        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          console.log(res.data);
          if (res.data.result != 'success') {
            wx.showModal({
              title: '温馨提示',
              content: res.data.data,
              success: function (res) {
                if (res.confirm) { //这里是点击了确定以后
                  console.log('用户点击确定')
                } else { //这里是点击了取消以后
                  console.log('用户点击取消')
                }
              }
            })
          } else {
            wx.showModal({
              title: '温馨提示',
              content: res.data.data,
              success: function (res) {
                if (res.confirm) { //这里是点击了确定以后
                  console.log('用户点击确定')
                } else { //这里是点击了取消以后
                  console.log('用户点击取消')
                }
              }
            })
          }
        }
      })
      this.data.flag = false
      setTimeout(() => {
        console.log('-----------------', 1);
        this.data.flag = true
      }, 5000)




    }
  },



  but_jq() {
    this.setData({
      showView_a: false
    })
  },


  //获取单个奖券的数据
  but_shu: function (e) {
    console.log(e.currentTarget.dataset.item.moshi);
    console.log(e.currentTarget.dataset.item.zhuangtai);
    if (e.currentTarget.dataset.item.zhuangtai == 2) {
      this.setData({
        cunsi: false,
        showView_a: true,
        moshi: e.currentTarget.dataset.item.moshi,
        ACTIVITY_TYPE: e.currentTarget.dataset.item.zhuangtai,
      })
    }else{
      this.setData({
        showView_a: true,
        moshi: e.currentTarget.dataset.item.moshi,
        ACTIVITY_TYPE: e.currentTarget.dataset.item.zhuangtai,
      })

    }


 


  },











  cunkuan() {
    this.setData({
      xunriqi: false
    })
  },



  //获取单个奖券的数据
  quanmian: function (e) {
    console.log(e.currentTarget.dataset.item.cunqu);
    console.log(e.currentTarget.dataset.item.cunquan);
    this.setData({
      xunriqi: true,
      cunqu: e.currentTarget.dataset.item.cunqu,
      ACTIVITY_REGULARLY: e.currentTarget.dataset.item.cunquan,
    })
  },





  handleInput(e) {

    console.log(e);
    let value = this.validateNumber(e.detail.value)

    console.log(value);
    this.setData({
      ACTIVITY_AMOUNT: value
    })
  },
  validateNumber(val) {
    return val.replace(/\D/g, '')
  },



















  //验证手机号
  mobileInput(e) {
    let value = e.detail.value.replace(/\D/g, '')
    this.setData({
      mobile: value,
    })
    // var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(19[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(19[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (this.data.mobile.length == 0) {
      wx.showToast({
        title: '输入的手机号为空，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        USER_ASSETS_IDCARD: '',
        mobileFormat: false,
      })
    } else if (this.data.mobile.length < 11) {

      wx.showToast({
        title: '手机号长度有误，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        USER_ASSETS_IDCARD: '',
        mobileFormat: false,
      })

    } else if (!myreg.test(this.data.mobile)) {
      wx.showToast({
        title: '手机号有误，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        USER_ASSETS_IDCARD: '',
        mobileFormat: false,
      })

    } else {
      this.setData({
        mobileFormat: true,
      })
    }
  },





  //验证身份证

  idcardblur: function (e) {
    let idcard = e.detail.value;
    let reg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;

    if (reg.test(idcard) == false) {
      this.setData({
        USER_IDCARD: ''
      })
      wx.showToast({
        title: '身份证号码有误',
        icon: 'none',
        duration: 3000
      })
    } else {
      this.setData({
        card_id: idcard
      })

    }

  },





  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

    //如果没数据就跳回登录页  

    // if (!this.data.mealsList) {
    //   wx.navigateTo({
    //     url: '../../pages/bjygdenglu/bjygdenglu'
    //   })
    // }

    // this.handleInput()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})